package cs2114.antiFingerMines;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import java.util.Observable;
import java.util.Observer;

/**
 * // -------------------------------------------------------------------------
 * /** The View for the AntiFingerMines game.
 * 
 * @author Blake Whaley
 * @version Apr 1, 2012
 */
public class AntiFingerMinesView
    extends View
{
    private MineField field;
    private Paint     border;
    private TextView  statusLabel;


    /**
     * Constructor for the View.
     * 
     * @param context
     *            the context.
     * @param attrs
     *            the attribute set.
     */
    public AntiFingerMinesView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        statusLabel = (TextView)findViewById(R.id.statusLabel);
    }


    /**
     * What do we want here? I think the position should be represented by a
     * dot.
     * 
     * @canvas the canvas the view is drawn on.
     */
    public void onDraw(Canvas canvas)
    {
        Paint paint = new Paint();
        paint.setColor(Color.CYAN);
        for (int i = 0; i < field.size(); i++)
        {
            canvas.drawCircle(
                field.getMine(i).getX(),
                field.getMine(i).getY(),
                field.getMine(i).getRadius(),
                paint);
        }

        Paint green = new Paint();
        green.setColor(Color.GREEN);
        green.setStyle(Paint.Style.FILL);

        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(
            0,
            field.getEndOfField(),
            getWidth(),
            getHeight(),
            green);
        canvas.drawCircle(field.getFingerLocation().getXloc(), field
            .getFingerLocation().getYloc(), 20, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        boolean handled = false;
        if (event.getAction() == MotionEvent.ACTION_DOWN
            && event.getX() > field.getFingerLocation().getXloc() - 15
            && event.getX() < field.getFingerLocation().getXloc() + 15
            && event.getY() > field.getFingerLocation().getYloc() - 15
            && event.getY() < field.getFingerLocation().getYloc() + 15)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            field.setFingerLocation(event.getX(), event.getY());
            handled = true;
        }
        return handled;
    }


    /**
     * Sets the model.
     * 
     * @param field
     */
    public void setModel(MineField field)
    {
        ;
        this.field = field;

        this.field.addObserver(new MineObserver());
    }


    private class MineObserver
        implements Observer
    {
        public void update(Observable observable, Object data)
        {
            invalidate();
        }
    }

}
